import Vue from 'vue';
import Router from 'vue-router';
import Home from "../views/Home.vue";
import Login from "../views/Login.vue";
import Category from "../views/Category.vue";
import Detail from "../views/Detail.vue";
import Cart from "../views/Carts";
import Profile from "../views/Profile";
import Order from "../views/Order";
import Addressed from "../views/Addressed";
import Addsite from "../views/Addsite";
import OrderDetail from "../views/OrderDetail.vue";
import OrderList from "../views/OrderList.vue";
import ProfileSet from "../views/UserSet.vue";
import Aaa from "../views/Aaaa.vue";

Vue.use(Router);

let router = new Router({
  routes: [
      {path:"/",component:Home, meta: { keepAlive: true }},//meta可以在路由里藏数据
      {path:"/category",component:Category, meta: { keepAlive: true }},
      {path:"/login",component:Login},
      {path:"/detail",component:Detail},
      {path:"/cart",component:Cart, meta: { keepAlive: true }},
      {path:"/profile",component:Profile,meta: { keepAlive: true }},
      {path:"/order",component:Order},
      {path:"/addressed",component:Addressed},
      {path:"/addsite",component:Addsite},
      {path:"/order/detail",component:OrderDetail},
      {path:"/order/list",component:OrderList,meta: { keepAlive: true }},
      {path:"/profile/set",component:ProfileSet,},
      {path:"/aaa",component:Aaa,},
  ]
});
let isLogin = function(){//判断用户是否登录
    let cookie = document.cookie.split(";");
    let user = {};
    let login = false;
    if(cookie.length){
        for(let i=0;i<cookie.length;i++){
            let arr = cookie[i].split("=");
            if(arr[0] === "userinfo"){
                login = true
            }
        }
        if(login) return true;
        else return false;
    }else{ return false }
};
router.beforeEach((to,from,next)=>{
    document.cookie = "from="+from.path;
    if(to.path === "/login" && isLogin()){
        next('/')
    }else{
        next();
    }
});
export function RedirectToLogin(){//一个组件可以export无数次
    router.push({to:"/login",params:{ redirect:router.currentRoute.fullPath}})//跳转到/login并且用params存储当前的路由
}
export default router;//一个组件可以export default一次
